<template>
  <el-dialog
    :append-to-body="true"
    :visible.sync="dialog"
    :title="isAdd ? '新增' : '编辑'"
    width="520px"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-form-item label="仓库名称" prop="warehousename">
        <el-input v-model="form.warehousename" style="width: 350px;" />
      </el-form-item>
      <el-form-item label="仓库地址" prop="warehouselocation">
        <el-input v-model="form.warehouselocation" style="width: 350px;" />
      </el-form-item>
      <!-- <el-form-item label="仓库纬度">
        <el-input v-model="form.warehouselatitude" style="width: 350px;" />
      </el-form-item> -->
      <!-- <el-form-item label="仓库经度">
        <el-input v-model="form.warehouselongitude" style="width: 350px;" />
      </el-form-item> -->
      <el-form-item label="仓库管理员" prop="personname">
        <el-input v-model="form.personname" style="width: 350px;" />
      </el-form-item>
      <el-form-item label="联系方式" prop="warehouseadministratorphone">
        <el-input v-model="form.warehouseadministratorphone" style="width: 350px;" />
      </el-form-item>

      <!-- <el-form-item label="归属部门ID" > -->
      <!-- <el-input v-model="form.warehousedeptid" style="width: 350px;"/> -->
      <!-- </el-form-item> -->
      <el-form-item label="仓库归属">
        <el-input v-model="form.warehousedeptname" style="width: 350px;" />
      </el-form-item>
      <!-- <el-form-item label="管理员ID" >
        <el-input v-model="form.warehouseadministratorid" style="width: 350px;"/>
      </el-form-item>-->
      <!-- <el-form-item label="区域码" >
        <el-input v-model="form.areacode" style="width: 350px;"/>
      </el-form-item>-->
      <el-form-item label="仓库状态" >
        <el-select
          allow-create
          filterable
          default-first-option
          v-model="form.state"
          :style="style"
        >
          <el-option
            key="1"
            label="在用"
            value="1"
          />
          <el-option
            key="0"
            label="停用"
            value="0"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from "@/api/tWarehouse";
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {
        id: "",
        warehousename: "",
        warehouselocation: "",
        warehousedeptid: "",
        warehousedeptname: "",
        warehouselatitude: "",
        warehouselongitude: "",
        warehouseadministratorid: "",
        personname: "",
        warehouseadministratorphone: "",
        areacode: "",
        state: ''
      },
      rules: {
        warehousename: [
          { required: true, message: "请输入仓库名称", trigger: "blur" }
        ],
        warehouselocation: [
          { required: true, message: "请输入仓库地址", trigger: "blur" }
        ],
        personname: [
          { required: true, message: "请输入仓库管理员", trigger: "blur" }
        ],
        warehouseadministratorphone: [
          { required: true, message: "请输入联系方式", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const explorer = navigator.userAgent;
    if (explorer.indexOf("Chrome") >= 0) {
      this.style = "width: 200px";
    } else {
      this.style = "width: 172px";
    }
  },
  methods: {
    cancel() {
      this.resetForm();
    },
    doSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.state ==="在用"){
            this.form.state = 1
          }else if(this.form.state === "停用"){
            this.form.state = 0
          }
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        }
      });
    },
    doAdd() {
      add(this.form)
        .then(res => {
          this.resetForm();
          this.$notify({
            title: "添加成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.$parent.$parent.init();
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    doEdit() {
      edit(this.form)
        .then(res => {
          this.resetForm();
          this.$notify({
            title: "修改成功",
            type: "success",
            duration: 2500
          });
          this.loading = false;
          this.sup_this.init();
        })
        .catch(err => {
          this.loading = false;
          console.log(err.response.data.message);
        });
    },
    resetForm() {
      this.dialog = false;
      this.$refs["form"].resetFields();
      this.form = {
        id: "",
        warehousename: "",
        warehouselocation: "",
        warehousedeptid: "",
        warehousedeptname: "",
        warehouselatitude: "",
        warehouselongitude: "",
        warehouseadministratorid: "",
        personname: "",
        warehouseadministratorphone: "",
        areacode: "",
        state: ""
      };
    }
  }
};
</script>

<style scoped>
</style>
